<?php
/**
 * @name myMsSQL Project
 * @copyright GPL
 * @author Hakan DAMAR
 * @abstract USER Controller Class
 */
require_once 'interface/IUSER.php';
require_once 'SYSTEM.php';

final class USER  implements IUSER{
	public static $DB;
	
  	public function __construct(){
  		if(!isset($_SESSION)){
  			header("Location:Error.php?ErrID=001&Response=USER");
  		}else{
  			$this->__isset(!USER::$DB) ? USER::$DB = DB::_getDBObject() : null;
  		}
  	}
  	
  	/**
  	 * (non-PHPdoc)
  	 * @see lib/interface/IUSER#createUser($username, $password, $authorityLevel, $email)
  	 */
  	public function createUser($username,$password,$authorityLevel,$email){
  		// Convert to Safe Data Controller
  		$username       = SYSTEM::inputController($username);
  		$password       = SYSTEM::inputController($password);
  		$authorityLevel = SYSTEM::inputController($authorityLevel);
  		$email          = SYSTEM::inputController($email); 
  		
  		// Data Type Controller
  		SYSTEM::typeController($username) 		!= "String" ? exit("USER->createUser() - username => Need String Data Type!")        : null;
  		SYSTEM::typeController($password) 		!= "String" ? exit("USER->createUser() - password => Need String Data Type!")        : null;
  		SYSTEM::typeController($authorityLevel) != "Int"    ? exit("USER->createUser() - authorityLevel => Need Integer Data Type!") : null;
  		SYSTEM::typeController($email)			!= "String" ? exit("USER->createUser() - email => Need String Data Type!")           : null;
  		  		
  		$newPassword = SYSTEM::customHash($password);
  		USER::$DB->execQuery("INSERT INTO tbl_user(username,password,email,authorityLevel) 
  								VALUES('$username','$newPassword','$email','$authorityLevel')");  		
  		return true;
  	}
  	
  	/**
  	 * (non-PHPdoc)
  	 * @see lib/interface/IUSER#setUserDetail($address, $telephone, $mobilePhone, $avatar, $title)
  	 */
  	public function setUserDetail($address,$telephone,$mobilePhone,$avatar,$title){
  		// Convert to Safe Data Controller
  		$address     = SYSTEM::inputController($address);
  		$telephone   = SYSTEM::inputController($telephone);
  		$mobilePhone = SYSTEM::inputController($mobilePhone);
  		$avatar      = SYSTEM::inputController($avatar);
  		$title       = SYSTEM::inputController($title);
  		
  		// Data Type Controller
  		SYSTEM::typeController($address)     != "String"  ? exit("USER->setUserDetail() - address => Need String Data Type!")        : null;
  		SYSTEM::typeController($telephone)   != "Numeric" ? exit("USER->setUserDetail() - telephone => Need Only Numeric String!")   : null;
  		SYSTEM::typeController($mobilePhone) != "Numeric" ? exit("USER->setUserDetail() - mobilePhone => Need Only Numeric String!") : null;
  		SYSTEM::typeController($avatar)      != "String"  ? exit("USER->setUserDetail() - avatar => Only input String avatar path!") : null;
  		SYSTEM::typeController($title)       != "String"  ? exit("USER->setUserDetail() - title => Need String Data Type!")          : null;
  	}
  	
  	public function getUserLoginInfo($UID){
  		$UID = SYSTEM::inputController($UID);
  		$sqlCmd = USER::$DB->execQuery("SELECT username,email,authorityLevel FROM tbl_user WHERE userID = $UID");
  		$sqlCmd != true ? EXCEPTION_HANDLER::_setMsg(8) : null;
  		while($datarow=sqlsrv_fetch_array($sqlCmd)){
	  		$login_username = $datarow["username"];
	  		$login_email    = $datarow["email"];
	  		$login_level    = $datarow["authorityLevel"];
	  	}
  		
  		// Add Session
  		$_SESSION["loginInfo"]["username"] = $login_username;
  		$_SESSION["loginInfo"]["email"]    = $login_email;
  		$_SESSION["loginInfo"]["level"]    = $login_level;
  	}
  	
  	public function getUserDetail($UID){
  		
  	}
  	
 	/**
 	 * Object exist controller
 	 * @param $objectName
 	 * @return void
 	 */
 	private function __isset($objectName){
 		return isset($objectName);
 	}
  
}


?>